layui.use(['form','layer','table'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#topicList',
        url : 'list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 10,
        limits : [10,15,20,25],
        id : "topicListTable",
        cols : [[
            {field: 'id', title: 'ID', width:100, align:"center"},
            {field: 'topicName', title: '名称', align:"center"},

            {field: 'articalNum', title: '文章数量（篇）', align:"center"},
            {title: '操作', width:250, templet:'#topicListBar',fixed:"right",align:"center"}
        ]]
    });
    

    //添加文章
    function addTopic(){
        var index = layui.layer.open({
            title : "添加专题",
            type : 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area : ["400px","350px"],
            content : "topicAdd",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }

    function updateTopic(data){
        var index = layui.layer.open({
            title : "修改专题",
            type : 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area : ["400px","350px"],
            content : "topicUpdate?id="+data.id,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }

    $(".addTopic_btn").click(function(){
        addTopic();
    })

    //列表操作
    table.on('tool(topicList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            updateTopic(data);
        } else if(layEvent === 'del'){
            //删除
            if(data.articalNum > 0){
                layer.msg("该专题下存在文章，不允许删除！",{icon: 5});
                return;
            }else{
                layer.confirm('确定删除此专题？',{icon:3, title:'提示信息'},function(index){
                    $.post("delete",{
                        id : data.id  //将需要删除的newsId作为参数传入
                    },function(data){
                        data = JSON.parse(data)
                        if(data.flag ==="success"){
                            layer.msg("删除成功");
                            tableIns.reload();
                            layer.close(index);
                        }else if(data.flag === "permissions"){
                            layer.msg("权限不足，无法删除！",{icon: 5});
                        }else{
                            layer.msg("删除失败！", {icon: 2});
                        }
                    })
                });
            }
        }
    });
})